package com.mz.demo.utils.tools;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mz.demo.dao.LoginMapper;
import com.mz.demo.entity.SysUser;
import com.mz.demo.utils.entity.GloableException;
import com.mz.demo.utils.entity.LoginUser;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private LoginMapper loginMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        LambdaQueryWrapper<SysUser> lqs = new LambdaQueryWrapper<>();
        lqs.eq(SysUser::getUsername, username );
        SysUser sysUser = loginMapper.selectOne(lqs);
        if( sysUser == null ){
            throw GloableException.gen(10001, "用户不存在");
        }

        LoginUser loginUser = new LoginUser( sysUser );
        return loginUser;
    }
}
